/**
 * @file Driver_RTC.c
 * @brief 从keysking的代码中移植
 * @author 2bam 
 * @version 1.0
 * @date 2024-12-29
 * @copyright Copyright (c) 2024  NULL
 * 
 * @change log:
 * Date           Version Author   Describe
 * 2024-12-29     1.0     2bam     first version
 */



/* Includes -------------------------------------------------------------------*/
#include "Driver_rtc.h"
/* define ---------------------------------------------------------------------*/
// RTC已经被初始化的值 记录在RTC_BKP_DR1中
#define RTC_INIT_FLAG 0x2333
/* typedef --------------------------------------------------------------------*/

/* variables ------------------------------------------------------------------*/
struct rt_thread rtc_thread;
static uint8_t rtc_stack[1024];
char time_date[100] = {0};
/* Private function prototypes ------------------------------------------------*/
static void ShowRTCTime(void *para);




/**
 * @brief 初始化RTC时钟
 * @date 2024-12-29
 * @author 2bam 
 */
static int KK_RTC_Init(void)
{
    rt_err_t ret;
    

    /** 初始化线程  */
    ret = rt_thread_init(&rtc_thread,
                        "rtc thread",
                        ShowRTCTime, NULL,
                        rtc_stack, sizeof(rtc_stack),
                        RT_THREAD_PRIORITY_RTC, 5);
    rt_thread_startup(&rtc_thread);


    return ret;
}
INIT_APP_EXPORT(KK_RTC_Init);

/**
 * @brief 
 * @param  para        My Param doc
 * @date 2024-12-29
 * @author 2bam 
 */
static void ShowRTCTime(void *para)
{
    RTC_TimeTypeDef cur_time;
    RTC_DateTypeDef cur_date;

    while (1)
    {
        HAL_RTC_GetTime(&hrtc,&cur_time,RTC_FORMAT_BIN);
        HAL_RTC_GetDate(&hrtc,&cur_date,RTC_FORMAT_BIN);
           
        
        rt_kprintf("%d-%d-%d %02d:%02d:%02d\n",
            cur_date.Year,cur_date.Month,cur_date.Date,
            cur_time.Hours,cur_time.Minutes,cur_time.Seconds);

        sprintf(time_date,"%d-%02d-%02d %02d:%02d:%02d",
            cur_date.Year+2025,cur_date.Month,cur_date.Date,
            cur_time.Hours,cur_time.Minutes,cur_time.Seconds);
        //OLED_PrintASCIIString(0,24,mess,&afont12x6,OLED_COLOR_REVERSED);
        //OLED_ShowFrame();
        
        HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
        rt_thread_mdelay(1000); 
        
    }
}

